#ifndef DFM_H
#define DFM_H

#include <stdint.h>
#include "LogicPool/LogicInterface.h"

class DFM {

  protected:
  
    LogicInterface & li;
    uint16_t limit[8];
    uint16_t delay[8];
      
  public:

    double pulses[8];  // pulses per liter and minute

    DFM(LogicInterface &interface, bool read_hw = false) : li(interface) 
    {
        for (int i=0; i<8; i++) {
            delay[i] = 0;
            limit[i] = (read_hw ? GetLimit(i) : 0);
        }
        SetPulses(752.); // optical Parker sensors
    };
    
    uint32_t GetID();
    void Reset();
    
    uint8_t GetState();
    bool GetState(uint8_t channel);
    
    void SetLimit(uint8_t channel, uint16_t limit);
    uint16_t GetLimit(uint8_t channel);
    void SetFlowLimit(uint8_t channel, double limit);
    double GetFlowLimit(uint8_t channel);

    void SetReactionTime(uint16_t time);
    void SetReactionTime(uint8_t channel, uint16_t time);
    uint16_t GetReactionTime(uint8_t channel) { return delay[channel]; };

    uint16_t GetRate(uint8_t channel);
    double GetFlow(uint8_t channel);
    
	void SetPulses(double ipulses);
	void SetPulses(uint8_t channel, double ipulses);
	double GetPulses(uint8_t channel = 0);
	
    //uint16_t Pulses(double flow) { return Pulses(0, flow); };
    uint16_t Pulses(uint8_t channel, double flow) { return pulses[channel] * flow; };
    //double Flow(uint16_t pulses) { return Flow(0, pulses); }; // flow in liters/min
    double Flow(uint8_t channel, uint16_t puls) { return (double)puls / pulses[channel]; }; // flow in liters/min

};


#endif
